import Config
import Examiner
import time

"""
获取日期文件夹路径 datatime: 
datetype = 'day' or 'month' or 'year'
datatime = 'yyyyMMdd or yyyyMM or yyyy'
"""


def get_date_folder_path(datetype, datatime):
    if datetype == Config.DATE_TYPE_DAY:
        return f'{datatime[0:4]}/{datatime[4:6]}'
    elif datetype == Config.DATE_TYPE_MONTH:
        return f'{datatime[0:4]}'
    else:
        return None


"""
获取count_bucket写入地址
datetype = 'day' or 'month' or 'year'
datatime = 'yyyyMMdd or yyyyMM or yyyy'
"""


def get_count_bucket_write_path(datetype, datatime):
    # 校验datatime,datetype是否合法，不合法抛出异常方法调用中止
    Examiner.check_datatime_for_datetype(datetype, datatime)
    # 查询文件是否存在，存在返回文件名，不存在返日期回默认文件名
    file_path = Examiner.check_count_file_exists_by_date_and_datetype(datetype, datatime)
    if file_path:
        return file_path
    elif ((datetype == Config.DATE_TYPE_DAY)
          or (datetype == Config.DATE_TYPE_MONTH)
          or (datetype == Config.DATE_TYPE_YEAR)):
        return (f'{Config.COUNT_BUCKET_FILE_HOME_PATH}'
                f'/{datetype}/{get_date_folder_path(datetype, datatime)}'
                f'/{Config.COUNT_FILE_NAME_PREFIX}{datatime}'
                f'{Config.SYSTEM_STORE_FILE_SUFFIX}')
    else:
        return None


"""
获取record_bucket写入地址
datetype = 'day' or 'month' or 'year'
datatime = 'yyyyMMdd or yyyyMM or yyyy'
"""


def get_record_bucket_write_path(datatime):
    # 校验datatime,datetype是否合法，不合法抛出异常方法调用中止
    Examiner.check_datatime_for_datetype(Config.DATE_TYPE_DAY, datatime)
    return (f'{Config.RECORD_BUCKET_FILE_HOME_PATH}'
            f'/{get_date_folder_path(Config.DATE_TYPE_DAY, datatime)}'
            f'/{Config.RECORD_FILE_NAME_PREFIX}{datatime}'
            f'{Config.SYSTEM_STORE_FILE_SUFFIX}')


"""
根据时间戳获取时分秒毫秒数据
"""


def get_timestamp_remove_yyyymmdd(timestamp):
    # 获取当前本地时间
    local_time = time.localtime(timestamp)

    # 提取时分秒
    hour = local_time.tm_hour
    minute = local_time.tm_min
    second = local_time.tm_sec

    # 提取毫秒
    milliseconds = int((timestamp - int(timestamp)) * 1000)

    # 格式化返回
    return f"{hour:02}{minute:02}{second:02}{milliseconds:03}"


def loger(msg):
    print(f'{time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())}{Config.LOG_SPLIT_CHAR}{msg}')
